package com.gz.mylife.app;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.adapter.BaseAdapterHelper;
import com.gz.mylife.adapter.QuickAdapter;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.listener.GetHousesAddressListener;
import com.zl.smartmall.library.po.HousesInfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SelectHouseActivity extends BaseActivity implements
		OnItemClickListener, OnClickListener, OnLoadingCoverRefreshListener {
	private PullToRefreshListView mListView;
	private QuickAdapter<HousesInfo> houseAdapter;
	private LoadingCover mloadCover;
	private List<HousesInfo> housesInfos;
	private HousesListener housesListener;
	private int currentAddress = -1;
	private MenuItem ensureItem;
	private TextView titile;
	private int buildingid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcommunity);
		titile = (TextView)findViewById(R.id.titile);
		titile.setText("请选择您所在的房间");
		buildingid = getIntent().getIntExtra("buildingid", 0);
		housesInfos = new ArrayList<HousesInfo>();
		setActionbarTitle("选择房间", true);
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.updata_menu, menu);
		ensureItem = menu.findItem(R.id.action_updata);
		// ensureItem = menu.add(0, 100, 0, "确定");
		MenuItemCompat.setShowAsAction(ensureItem,
				MenuItemCompat.SHOW_AS_ACTION_ALWAYS
						| MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		if (currentAddress == -1) {
			// ensureItem.setVisible(false);
			setActionbarTitle("选择房间号", false);
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_updata:
			if (currentAddress == -1) {
				Toast.makeText(this, "请先选择房间", Toast.LENGTH_SHORT).show();
				return true;
			}
			Intent intent = new Intent();
			intent.putExtra("obj", (Serializable)housesInfos.get(currentAddress));
			setResult(106, intent);
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initView() {
		mloadCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mloadCover.setOnLoadingCoverRefreshListener(this);
		mListView = (PullToRefreshListView) findViewById(R.id.lv_community);
		houseAdapter = houseAdapter();
		mListView.setAdapter(houseAdapter);
		mListView.setMode(Mode.DISABLED);
		mListView.setOnItemClickListener(this);
		housesListener = new HousesListener(this);
		loadbuildingData();
	}

	// 获取房屋的信息
	private void loadbuildingData() {
		IGZAddress.getInstance().getHouses(buildingid, housesListener);
	}

	private QuickAdapter<HousesInfo> houseAdapter() {
		return new QuickAdapter<HousesInfo>(getApplicationContext(),
				R.layout.selectcommunity_item, housesInfos) {

			@Override
			protected void convert(BaseAdapterHelper helper, HousesInfo item) {
				helper.setText(R.id.tv_name, item.getHouseaddress());
				ImageView ivselect = helper.getView(R.id.iv_select);
				if (item.getStatus() == 0) {
					ivselect.setVisibility(View.GONE);
				} else {
					ivselect.setVisibility(View.VISIBLE);
				}
			}
		};
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onLoadingCoverRefresh() {
		loadbuildingData();
	}

	private static class HousesListener extends GetHousesAddressListener {
		private SelectHouseActivity mOuter;

		public HousesListener(SelectHouseActivity outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<SelectHouseActivity>(mOuter);
		}

		@Override
		public void onResponseSuccess(List<HousesInfo> lists) {
			if (mOuter == null) {
				return;
			}
			mOuter.housesInfos.clear();
			mOuter.housesInfos.addAll(lists);
			mOuter.houseAdapter.clear();
			mOuter.houseAdapter.addAll(mOuter.housesInfos);
			mOuter.mloadCover.onRefreshSuccess();
		}

		@Override
		public void onNoMoreData() {
			if (mOuter == null) {
				return;
			}
			mOuter.mloadCover.onRefreshFailed("暂无房屋数据");
		}

		@Override
		public void onNoNetWork() {
			if (mOuter == null) {
				return;
			}
			mOuter.mloadCover.onRefreshFailed();
			Toast.makeText(mOuter.getApplicationContext(),
					Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onResponseFailed() {
			if (mOuter == null) {
				return;
			}
			mOuter.mloadCover.onRefreshFailed();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (id >= 0) {
			if (id != currentAddress) {
				if (currentAddress != -1) {
					housesInfos.get(currentAddress).setStatus(0);
				} else {
					ensureItem.setVisible(true);
				}
				currentAddress = (int) id;
				housesInfos.get(currentAddress).setStatus(1);
			}
			houseAdapter.clear();
			houseAdapter.addAll(housesInfos);
		}
	}

}
